package com.ruicar.afs.cloud.afscase.loanapprove.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.BackReasonCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.CaseBackReasonInfo;
import com.ruicar.afs.cloud.afscase.loanapprove.service.CaseBackReasonInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.components.datadicsync.DicHelper;
import com.ruicar.afs.cloud.components.datadicsync.dto.DicDataDto;

import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.enums.BusiNodeEnum;

import com.ruicar.afs.cloud.image.enums.IsEnableRuleEnum;
import com.ruicar.afs.cloud.image.service.ComAttaManageService;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.image.vo.AuditFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author rongji.zhang
 * @Date 2020/8/17
 * @description
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/backReasonAllocation")
@Api("激活规则配置")
public class BackReasonAllocationController {

    private CaseBackReasonInfoService caseBackReasonInfoService;

    private ComAttaManageService comAttaManageService;

    private ComAttachmentFileService comAttachmentFileService;

    /**
     * 查询返回合作商原因列表信息
     * @Author rongji.zhang
     * @param condition
     * @return
     */
    @PostMapping(value = "/getBackReasonList")
    @ApiOperation(value = "查询返回合作商原因列表信息")
    public IResponse getBackReasonList(@RequestBody BackReasonCondition condition) {
        IPage<CaseBackReasonInfo> iPage =caseBackReasonInfoService.page(new Page(condition.getPageNumber(),condition.getPageSize()),Wrappers.<CaseBackReasonInfo>query().lambda()
                .like(EmptyUtils.isNotEmpty(condition.getAttachmentName()),CaseBackReasonInfo::getAttachmentName,condition.getAttachmentName())
                .like(EmptyUtils.isNotEmpty(condition.getBackWords()),CaseBackReasonInfo::getBackWords,condition.getBackWords())
                .orderByDesc(CaseBackReasonInfo::getCreateTime));
        return IResponse.success(iPage);
    }

    /**
     * 查询返回合作商原因列表信息
     * @Author rongji.zhang
     * @return
     */
    @PostMapping(value = "/getAttachmentNameList")
    @ApiOperation(value = "查询返回合作商原因列表信息")
    public IResponse getAttachmentNameList() {
        List<ComAttachmentManagement> managementList=comAttaManageService.list(Wrappers.<ComAttachmentManagement>lambdaQuery()
                .eq(ComAttachmentManagement::getBusiNode,BusiNodeEnum.LOAN_APPLY.getCode())
                .eq(ComAttachmentManagement::getAttachmentClass,3)
                .eq(ComAttachmentManagement::getIsEnableRule, IsEnableRuleEnum.YES.getCode()));

        Map<Long, List<ComAttachmentManagement>> groupMap = managementList.stream().collect(Collectors.groupingBy(ComAttachmentManagement::getParentId));
        Map<String, List<DicDataDto>> groupDicMap= DicHelper.getDicMaps("imageGroupUnicode");
        List<String> dicValues=new ArrayList<>();
        if(!CollectionUtils.isEmpty(groupDicMap)){
            dicValues=groupDicMap.get("imageGroupUnicode").stream().map(dicDataDto ->dicDataDto.getValue()).collect(Collectors.toList());
        }
        for (Map.Entry<Long, List<ComAttachmentManagement>> entry : groupMap.entrySet()) {
            ComAttachmentManagement management = comAttaManageService.getById(entry.getKey());
            if (dicValues.contains(management.getUniqueCode())) {
                managementList.add(management);
                managementList.removeAll(entry.getValue());
            }
        }
        List<AuditFileVo> auditFileVos=new ArrayList<>();
        managementList.forEach(m->{
            AuditFileVo vo=new AuditFileVo();
            BeanUtils.copyProperties(m,vo);
            vo.setAttachmentCode(m.getId().toString());
            auditFileVos.add(vo);
        });
        return IResponse.success(auditFileVos);
    }

    /**
     * 添加返回合作商原因信息
     * @Author rongji.zhang
     * @return
     */
    @PostMapping(value = "/addBackReason")
    @ApiOperation(value = "添加返回合作商原因信息")
    public IResponse addBackReason(@RequestBody CaseBackReasonInfo caseBackReasonInfo) {
        try {
            caseBackReasonInfoService.save(caseBackReasonInfo);
            return IResponse.success("操作成功！");
        }catch (Exception e){
            return IResponse.fail("操作失败,"+e);
        }
    }

    /**
     * 修改返回合作商原因信息
     * @Author rongji.zhang
     * @return
     */
    @PostMapping(value = "/editBackReason")
    @ApiOperation(value = "修改返回合作商原因信息")
    public IResponse editBackReason(@RequestBody CaseBackReasonInfo caseBackReasonInfo) {
        try {
            caseBackReasonInfoService.updateById(caseBackReasonInfo);
            return IResponse.success("操作成功！");
        }catch (Exception e){
            return IResponse.fail("操作失败,"+e);
        }
    }

    /**
     * 删除返回合作商原因信息
     * @Author rongji.zhang
     * @return
     */
    @PostMapping(value = "/deleteBackReason")
    @ApiOperation(value = "删除返回合作商原因信息")
    public IResponse deleteBackReason(@RequestBody CaseBackReasonInfo caseBackReasonInfo) {
        try {
            caseBackReasonInfoService.removeById(caseBackReasonInfo.getId());
            return IResponse.success("操作成功！");
        }catch (Exception e){
            return IResponse.fail("操作失败,"+e);
        }
    }
}
